

enum MissionStatus {


    UNACCEPTABLE = 0,

    CAN_ACCEPT = 1,

    DURGING = 2,

    CAN_SUBMIT = 3,

    FINISHIED = 4
}

class Mission {

    id = 0;

    title = '';
    needLevel = 0;

    fromNpcId = 0;

    toNpcId = 0;

    isAccepted = false;

    isSubmit = false;

    current = 0;
    // missionRewardsId=0;

    total = 1;
    AcceptStatements:any[]=[];
    SubmitStatements:any[]=[];

    canAcceptContent: any[] = [];

    canSubmitContent: any[] = [];

    constructor(data:any,config:any,Config:any){
        this.id = data.id;
        this.title = data.title;
        // this.missionRewardsId=data.missionRewardsId;
        console.log(111);
        console.log(config);
        console.log(Config);
        console.log(111);
        // this.AcceptStatements = config.canAcceptContent
        // this.SubmitStatements= config.canSubmitContent;
        this.needLevel = data.needLevel;
        this.fromNpcId = data.fromNpcId;
        this.toNpcId = data.toNpcId;
        this.rewards = data.rewards;
        for(let item of config){
            if(item.id==this.id){
                console.log(555666);
                this.AcceptStatements.push(item);
            }
        }
        for(let item1 of Config){
            if(item1.id==this.id){
                this.SubmitStatements.push(item1);
            }
        }
        this.changeOrder();
        console.log(66);
        console.log(this.AcceptStatements);
        console.log(this.SubmitStatements);
        console.log(66);
        
    }

    // parseFromConfig(config: any) {
       
    // }
    changeOrder(){
        for(let i=0;i<this.AcceptStatements.length;i++){
            for(let a of this.AcceptStatements){
                if(a.order==i){
                    this.canAcceptContent[i]=a;
                }
            }
            for(let b of this.SubmitStatements){
                if(b.order==i){
                    this.canSubmitContent[i]=b;
                }
            }
        }
    }



   


    rewards: { type: string, num: number }[] = [];




    status: MissionStatus = 0;

    update() {
        let nextStatus: MissionStatus = 0;
        if (this.isSubmit) {
            nextStatus = MissionStatus.FINISHIED
        }
        else if (this.isAccepted) {
            if (this.current >= this.total) {
                nextStatus = MissionStatus.CAN_SUBMIT;
            }
            else {
                nextStatus = MissionStatus.DURGING;
            }

        }
        else {
            if (this.needLevel > user.level) {
                nextStatus = MissionStatus.UNACCEPTABLE;
            }
            else {
                nextStatus = MissionStatus.CAN_ACCEPT;
            }

        }


        if (nextStatus != this.status) {
            this.status = nextStatus;
            return true;
        }
        else {
            return false;
        }

    }
}


class MissionManager extends EventDispatcher {


    missions: Mission[] = [];

    constructor() {
        super();
    }

    acceptMission(mission: Mission) {
        mission.isAccepted = true;
        mission.current = 1;
        this.update();
    }

    parseFromConfig(config: any) {
        for (let item of config.tasks) {
            const mission1 = new Mission(item,config.canAcceptContent,config.canSubmitContent);
            // mission1.parseFromConfig(item);
            missionManager.missions.push(mission1);
            console.log(this.missions);
        }
    }

    submitMission(mission: Mission) {
        mission.isSubmit = true;
        let num=0;
        this.update();
        const rewards = mission.rewards;
        for (let reward of rewards) {
            if (reward.type == 'level') {
                user.level += reward.num;
            }
            else if (reward.type == 'sliverCoin') {
                user.sliverCoin += reward.num;
            }
            else if(reward.type=="potion"){
                num=reward.num;
              }
        }
        // if(mission.id==mission.missionRewardsId){
            
            for(let item of itemManager.Items){
                if(num==0){
                    break;
                }
                else{
                 if(item.id==4){
                    num--;
                    packagedItems.push(item);
                 }
                }
               
            }
        //}
        user.dispatchEvent("userchange","111");
    }

    init() {
        user.addEventListener("userchange", () => {
            this.update();
        });
        this.update();
    }

    update() {
        let needUpdate = false;
        for (let mission of this.missions) {
            const missionNeedUpdate = mission.update();
            if (missionNeedUpdate) {
                needUpdate = true;
            }
        }
        if (needUpdate) {
            this.dispatchEvent("missionUpdate", {});
        }
    }


}


const missionManager = new MissionManager();